const mysql = require('mysql2/promise');
const config = require('../config/db.config');

async function setupTestData() {
  let connection;
  try {
    // 移除环境特定的配置以避免警告
    const { development, test, production, ...dbConfig } = config;
    
    connection = await mysql.createConnection({
      ...dbConfig,
      connectTimeout: 10000,
      enableKeepAlive: true,
      keepAliveInitialDelay: 0
    });

    // 创建测试用户
    await connection.execute(`
      INSERT INTO users (username, password, nickname, avatar_type) 
      VALUES ('testuser', '111111', '测试用户', 'default')
      ON DUPLICATE KEY UPDATE password = '111111'
    `);

    // 创建测试管理员
    await connection.execute(`
      INSERT INTO admins (username, password, nickname) 
      VALUES ('admin', '111111', '管理员')
      ON DUPLICATE KEY UPDATE password = '111111'
    `);

    console.log('测试数据初始化成功');
  } catch (err) {
    console.error('测试数据初始化失败:', err.message);
    throw err;
  } finally {
    if (connection) {
      await connection.end();
    }
  }
}

// 如果直接运行此文件，则执行初始化
if (require.main === module) {
  setupTestData().catch(err => {
    console.error('初始化失败:', err);
    process.exit(1);
  });
}

module.exports = setupTestData; 